// Cypress测试支持文件

// 导入Cypress命令
import './commands';

// 添加全局hooks
beforeEach(() => {
  // 清理localStorage
  cy.clearLocalStorage();
  cy.clearCookies();
  
  // 拦截未处理的异常
  Cypress.on('uncaught:exception', (err, runnable) => {
    // 这里可以根据需要处理异常
    console.error('Uncaught Exception:', err);
    return false; // 阻止测试失败
  });
});

// 添加全局测试配置
Cypress.config('defaultCommandTimeout', 10000);
Cypress.config('requestTimeout', 10000);
Cypress.config('responseTimeout', 10000);

// 全局测试数据
Cypress.Commands.add('getTestData', () => {
  return {
    agent: {
      name: 'Test Agent',
      description: 'Test Description',
      systemPrompt: 'You are a helpful assistant',
      modelConfigId: 'test-config-id',
      isActive: true,
    },
    modelConfig: {
      name: 'Test Model Config',
      provider: 'OpenAI',
      model: 'gpt-4',
      maxTokens: 4000,
      temperature: 0.7,
      isActive: true,
    },
    user: {
      email: 'test@example.com',
      password: 'password123',
    },
  };
});

// 添加API mock命令
Cypress.Commands.add('mockApiResponse', (method, url, response, statusCode = 200) => {
  cy.intercept(method, url, {
    statusCode,
    body: response,
  }).as(`${method}-${url}`);
});

// 添加等待API响应命令
Cypress.Commands.add('waitForApiResponse', (alias) => {
  cy.wait(`@${alias}`);
});

// 添加登录命令
Cypress.Commands.add('login', (email = 'test@example.com', password = 'password123') => {
  cy.session([email, password], () => {
    cy.visit('/login');
    cy.get('input[name="email"]').type(email);
    cy.get('input[name="password"]').type(password);
    cy.get('button[type="submit"]').click();
    cy.url().should('not.include', '/login');
  });
});

// 添加创建智能体命令
Cypress.Commands.add('createAgent', (agentData) => {
  cy.visit('/agents/create');
  cy.get('input[name="name"]').type(agentData.name);
  cy.get('textarea[name="description"]').type(agentData.description);
  cy.get('textarea[name="systemPrompt"]').type(agentData.systemPrompt);
  cy.get('select[name="modelConfigId"]').select(agentData.modelConfigId);
  cy.get('button[type="submit"]').click();
  cy.url().should('include', '/agents');
});

// 添加等待加载完成命令
Cypress.Commands.add('waitForLoading', () => {
  cy.get('.ant-spin-spinning').should('not.exist');
});

// 添加获取通知消息命令
Cypress.Commands.add('getNotification', () => {
  return cy.get('.ant-message');
});

// 添加验证成功消息命令
Cypress.Commands.add('shouldHaveSuccessMessage', (message) => {
  cy.getNotification().should('contain', message).and('have.class', 'ant-message-success');
});

// 添加验证错误消息命令
Cypress.Commands.add('shouldHaveErrorMessage', (message) => {
  cy.getNotification().should('contain', message).and('have.class', 'ant-message-error');
});

// 添加表格操作命令
Cypress.Commands.add('getTableRows', () => {
  return cy.get('.ant-table-tbody tr');
});

// 添加获取表格行数据命令
Cypress.Commands.add('getTableRowData', (rowIndex = 0) => {
  return cy.getTableRows().eq(rowIndex).find('td');
});

// 添加点击表格操作按钮命令
Cypress.Commands.add('clickTableActionButton', (rowIndex, buttonText) => {
  cy.getTableRowData(rowIndex).within(() => {
    cy.contains('button', buttonText).click();
  });
});

// 添加验证模态框命令
Cypress.Commands.add('shouldHaveModal', (title) => {
  cy.get('.ant-modal').should('be.visible').within(() => {
    cy.get('.ant-modal-title').should('contain', title);
  });
});

// 添加关闭模态框命令
Cypress.Commands.add('closeModal', () => {
  cy.get('.ant-modal-close').click();
  cy.get('.ant-modal').should('not.exist');
});

// 添加验证面包屑导航命令
Cypress.Commands.add('shouldHaveBreadcrumb', (items) => {
  cy.get('.ant-breadcrumb').within(() => {
    items.forEach((item, index) => {
      cy.get('.ant-breadcrumb-link').eq(index).should('contain', item);
    });
  });
});